﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.CompilerServices;

namespace RealState.Utils
{
    public static class ObjectMethods
    {
        public static string NullToEmptyString(this object o)
        {
            if ((o != null) && (o != DBNull.Value))
            {
                return o.ToString();
            }
            return string.Empty;
        }

        public static bool NullToFalseValue(this object o)
        {
            if ((o == null) || (o == DBNull.Value))
            {
                return false;
            }
            if (o.ToString() == "0")
            {
                return false;
            }
            return ((o.ToString() == "1") || Convert.ToBoolean(o));
        }

        public static double NullToFloatOne(this object o)
        {
            if ((o != null) && (o != DBNull.Value))
            {
                return Convert.ToDouble(o);
            }
            return 1.0;
        }

        public static double NullToFloatZero(this object o)
        {
            if ((o != null) && (o != DBNull.Value))
            {
                return Convert.ToDouble(o);
            }
            return 0.0;
        }

        public static int NullToOne(this object o)
        {
            if ((o != null) && (o != DBNull.Value))
            {
                return Convert.ToInt32(o);
            }
            return 1;
        }

        public static bool NullToTrueValue(this object o)
        {
            if ((o == null) || (o == DBNull.Value))
            {
                return true;
            }
            if (o.ToString() == "0")
            {
                return false;
            }
            return ((o.ToString() == "1") || Convert.ToBoolean(o));
        }

        public static int NullToZero(this object o)
        {
            if ((o != null) && (o != DBNull.Value))
            {
                return Convert.ToInt32(o);
            }
            return 0;
        }
    }
}
